


*-------------------------------------------------------------------------------
* population data
*-------------------------------------------------------------------------------


* population: city of Chicago
clear 
set obs 120
gen month = tm(2010m6) if _n == 1
replace month = month[_n-1] + 1 if month[_n-1] ~= .
format month %tm
gen pop = 2695598 if _n == 1
replace pop = 2746388 if month == tm(2020m5)

	/*
	Population, Census, April 1, 2020	2,746,388
	Population, Census, April 1, 2010	2,695,598
	*/
	
gen ln_pob = ln(pop)
ipolate ln_pob month, gen(ln_pob_i)
gen pop_cgr = exp(ln_pob_i)
tempfile pop_chicago
savesome month pop_cgr using `pop_chicago'


* population: Cook County
import delimited "data/inputs/other/il cities population.csv", clear 
keep if name == "Cook County"
assert _N == 1
keep name popestimate*

reshape long popestimate, i(name) j(year, string)
gen month = tm(2020m4) if year == "042020"
replace year = "01/" + year if length(year) == 4
replace month = monthly(year, "MY") if month == .
format month %tm

egen countyid = group(name)
tsset countyid month
tsfill, full

gen ln_pob = ln(popest)
by countyid: ipolate ln_pob month, gen(ln_pob_i)
gen pop_cookcounty = exp(ln_pob_i) 

keep month pop_cookcounty

tempfile pop_cookcounty
savesome month pop using `pop_cookcounty'
	
	

*-------------------------------------------------------------------------------
* contact card data, ISR data, traffic stop data
*-------------------------------------------------------------------------------


* contact card data
use "data/outputs/CC_beat.dta", clear
collapse (sum) pedestrian, by(month)
tempfile cc
save `cc'


* traffic stop data
use "data/outputs/TSSS_beat.dta", clear

replace agencyname = "troopers" if regexm(agencyname, "ILLINOIS")
replace agencyname = "cpd" if regexm(agencyname, "CHICAGO")
replace agencyname = "suburbs" if agencyname ~= "troopers" & agencyname ~= "cpd"

collapse (sum) count, by(agencyname month)

reshape wide count, i(month) j(agencyname, string)
renvars count*, prefix(itss)

tempfile itss
save `itss'


* isr data
use "data/outputs/ISRpedestrian_beat.dta", clear
collapse (sum) isr_count_pedestrian, by(month)
tempfile isr
save `isr'
	
	

*-------------------------------------------------------------------------------
* merge
*-------------------------------------------------------------------------------

* merge
use `itss', clear
merge 1:1 month using `cc'
drop _m
merge 1:1 month using `isr'
drop _m
merge 1:1 month using `pop_chicago'
drop _m
merge 1:1 month using `pop_cookcounty'
	
	

*-------------------------------------------------------------------------------
* calculate rates
*-------------------------------------------------------------------------------

* shaded region
gen treatment_rates = 29.2 if (month >= tm(2015m8) & month <= tm(2016m1))
		
* annualized (x12) rates per 100 people
gen pedrate_cpd = 100 * 12 * pedestrian / pop_cgr
gen trafficrate_cpd = 100 * 12 * itsscountcpd / pop_cgr
gen trafficrate_troopers = 100 * 12 * itsscounttroopers / pop_cookcounty
	

*-------------------------------------------------------------------------------
* graph
*-------------------------------------------------------------------------------

#delimit;
twoway (area treatment_rates month if month >= tm(2012m1) & month <= tm(2020m2), 
		lcolor(gs15%100) fcolor(gs15%100))
	(connected trafficrate_troopers month  if month >= tm(2012m1) & month <= tm(2020m2),
		lcolor(gs8) mcolor(gs8) msymbol(triangle) msize(vsmall))
	(connected pedrate_cpd month if month >= tm(2012m1) & month <= tm(2020m2), 
		mcolor(black) msize(vsmall) lcolor(black) msymbol(O))
	(connected trafficrate_cpd month if month >= tm(2012m1) & month <= tm(2020m2), 
		lcolor(blue%80) lwidth(medthick) msymbol(square) mcolor(blue%80) msize(vsmall)), 
graphregion(fcolor(white) lcolor(white) margin(zero))
plotregion(fcolor(white) lstyle(none) lcolor(white) ilstyle(none))
xsize(7) ysize(5)
title("", 
	  color(black) placement(west) justification(left) size(medlarge)) 
ytitle("Stops per 100 per Year", size(medlarge))
yscale(lcolor(none))
ylabel(0(5)25, labsize(medlarge) glcolor(white) angle(horizontal))
xtitle("", size(medsmall)) 
xscale(lcolor(none))
xlabel(624(12)723, labsize(medlarge) format(%tmCY)) 
legend(off);
graph export "figures/Figure1.pdf", replace;

#delimit cr


*-------------------------------------------------------------------------------
* regression reported in text
*-------------------------------------------------------------------------------

keep month trafficrate_troopers trafficrate_cpd
reshape long trafficrate_, i(month) j(agency, string)
gen treat = (month >= tm(2016m1) & agency == "cpd")
reghdfe trafficrate treat, a(agency month) vce(r)





* End
